<template>
	<view class="prop">
		<!-- <view class="prop-box"> -->
<!--			<view style="background-color: white; width: 100%;height: var(&#45;&#45;status-bar-height);">-->
<!--			            &lt;!&ndash; 这里是状态栏 &ndash;&gt;-->
<!--				<uni-nav-bar color="#000" left-icon="left" @clickLeft="navBack()" title="网络配置" rightIcon="refresh" @clickRight="refreshPage()"/>-->
<!--			&lt;!&ndash; <button class="btn" type="primary" plain="true" @click="searchBle">刷新设备</button> &ndash;&gt;-->
<!--		</view>-->
		<view class="uni-padding-wrap uni-common-mt group">
			<!-- <view>
				<scroll-view scroll-y="true" class="scroll-Y">
					<button v-for="(item,index) in devices" class="scroll-view-item" style="color: #0081FF;"
						@click="onConn(item)">{{ item.name }}
					</button>
				</scroll-view>
			</view> -->
			<!-- <view @tap="goTop" class="uni-link uni-center uni-common-mt">
        点击这里返回顶部
      </view> -->
		</view>

		<!--    <view class="line">-->
		<!--      <view class="title">BSSID</view>-->
		<!--      <view class="maohao">:</view>-->
		<!--      <view class="password">{{ BSSID }}</view>-->
		<!--    </view>-->
		<view class="line">
			<view class="title">WIFI名</view>
			<view class="maohao">:</view>
			<view class="password">
				{{ SSID }}
				<!-- <input type="text" value="" v-model="SSID" placeholder="请输入WIFI账号" @input="changeSSID" /> -->
			</view>
		</view>
		<view class="line last">
			<view class="title">密码</view>
			<view class="maohao">:</view>
			<view class="password">
				<input v-if="password_icon==0" type="password" value="" v-model="password" placeholder="请输入WIFI密码" />
				<input v-else-if="password_icon==1" type="" value="" v-model="password" placeholder="请输入WIFI密码" />
				<image :src="item" v-for="(item,i) in passIcon" :key="item" @click="passwordIcon"></image>
			</view>
		</view>
		<view class="prop-box">
			<!-- <button class="btn" type="primary" plain="true" @click="senCode()">开始智能配网</button> -->
			<button class="add-btn" @click="senCode()">开始智能配网</button>
		</view>

	</view>
</template>

<script>
	// 导入插件
	import blueToothTool from "../../static/libs/BluetoothTool";

  const EspTouch = uni.requireNativePlugin('SZW-EspTouch');
	// import permission from '@/common/permission.js'
	import encoder from '@/static/libs/encoder.js'
	import bluetoothTool from '@/static/libs/BluetoothTool.js'

	export default {
		components: {},
		data() {
			return {
				SSID: '', //WIFI名称
				// BSSID: '48:0e:ec:fe:f8:b8',
				BSSID: '', //MAC地址
				password: '', //密码
				modal: '', //配网内容
				password_icon: '0', //掩码状态
				passIcon: ['../../static/password1.png'], //掩码图片
				devices: [],
				result: false,
				msg: '',
        gps:'close',
        buleTooth:'STATE_OFF'
			}
		},
		watch: {
			msg() {
				uni.showToast({
					title: this.msg
				})
			}
		},
		onShow() {
    if(this.gps === 'open'){
      this.getWifiInfo();
    }
		},
		onLoad() {

      var main = plus.android.runtimeMainActivity();
      var IntentFilter = plus.android.importClass('android.content.IntentFilter');
      var BroadcastReceiver = plus.android.importClass('android.content.BroadcastReceiver');
      var LocationManager = plus.android.importClass("android.location.LocationManager");

// 获取系统的位置管理器
      var locationManager = main.getSystemService(main.LOCATION_SERVICE);

// 创建监听 GPS 状态变化的广播接收器


// 注册 GPS 状态变化的广播接收器

		},
		onReady() {
			//#ifdef APP-PLUS
			// 蓝牙
			// uni.openBluetoothAdapter({
			// 	success : (res)=> {

				// }
				// fail:(res)=> {
        //   this.$api.msg('也许蓝牙和定位没开，或者没权限？');
        //
				// }
			// })

			//#endif
		},
		methods: {
			// upper: function (e) {
			//   console.log(e)
			// },
			// lower: function (e) {
			//   console.log(e)
			// },
			// scroll: function (e) {
			//   console.log(e)
			//   this.old.scrollTop = e.detail.scrollTop
			// },
			// goTop: function (e) {
			//   // 解决view层不同步的问题
			//   this.scrollTop = this.old.scrollTop
			//   this.$nextTick(function () {
			//     this.scrollTop = 0
			//   });
			//   uni.showToast({
			//     icon: "none",
			//     title: "纵向滚动 scrollTop 值已被修改为 0"
			//   })
			// },


			// 掩码状态
			passwordIcon() {
				if (this.password_icon == 0) {
					this.passIcon[0] = '../../static/password2.png'
					this.password_icon = 1
				} else if (this.password_icon == 1) {
					this.passIcon[0] = '../../static/password1.png'
					this.password_icon = 0
				}
			},

			// 当键盘输入时，触发input事件
			changeSSID(value) {
				console.log(value)
				console.log(this.SSID)
				this.getWifiInfo()
			},

			// Android 获取权限
			getJurisdictionInfo() {
				var main = plus.android.runtimeMainActivity();
				// this.jumpToLocationSettings();
				uni.showModal({
					content: '定位和WIFI权限没有开启，无法获取配网信息',
					showCancel: false,
					success: function(res) {

						if (res.confirm) {
							var main = plus.android.runtimeMainActivity();
							var Intent = plus.android.importClass('android.content.Intent');
							var Build = plus.android.importClass("android.os.Build");

							var Settings = plus.android.importClass("android.provider.Settings");
							var Uri = plus.android.importClass("android.net.Uri");
							var intent = new Intent();
							intent.setAction(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
							// var uri = Uri.fromParts("package", main.getPackageName(), null);
							// intent.setData(uri);
							// main.startActivity(intent);

						}
					}
					// }
				});
				// }
			},

			//  Android 开启WIFI
			loadWifiInfo() {
				uni.showModal({
					content: '请开启WIFI',
					showCancel: false,
					success: function(res) {
						console.log(res)
						if (res.confirm) {
							// 打开wifi设置页
							var main = plus.android.runtimeMainActivity();
							var Intent = plus.android.importClass("android.content.Intent");
							var mIntent = new Intent('android.settings.WIFI_SETTINGS');
							main.startActivity(mIntent);
						}
					}
				});
			},

			// JS 调用插件功能
			// 获取当前连接的 WiFi 信息


			// 开始智能配网
			// smartConfig() {
			//   let _this = this
			//   if (_this.password === '' || _this.SSID === '') {
			//     _this.$api.msg('请输入配网信息');
			//   } else {
			//     EspTouch.smartConfig({
			//       bssid: _this.BSSID,
			//       ssid: _this.SSID,
			//       password: _this.password,
			//       deviceCount: '1',
			//       broadcast: 'true'
			//     }, result => {
			//       console.log(result)
			//       console.log(_this.SSID, _this.password)
			//       const msg = JSON.stringify(result);
			//       console.log(msg)
			//
			//       switch (result.type) {
			//         case 'begin':
			//           _this.modal = "正在配网..."
			//           break;
			//         case 'stepOk':
			//           console.log(`stepOk: ${JSON.stringify(result)}`);
			//           _this.modal = "配置成功" + msg + "..."
			//           console.log('配置成功' + msg)
			//           break;
			//         case 'stepFailed':
			//           console.log(`stepFailed: ${JSON.stringify(result)}`);
			//           _this.modal = "配置失败" + msg + "..."
			//           console.log('配置失败' + msg)
			//           break;
			//         case 'end':
			//           // _this.modal = "配置完成，请重启设备验证是否配置成功"
			//           _this.modal = "配置完成，请重启设备验证是否配置成功"
			//           console.log(`end: ${JSON.stringify(result)}`);
			//           break;
			//         default:
			//           break;
			//
			//       }
			//
			//       uni.showModal({
			//         title: 'WIFI配网',
			//         content: _this.modal,
			//         showCancel: false,
			//         confirmText: _this.modal != '配置完成，请重启设备验证是否配置成功' ? '取消配网' : '确定',
			//         success: function (res) {
			//           console.log(res)
			//           if (res.confirm) {
			//             console.log('用户点击确定');
			//             if (_this.modal != '配置完成，请重启设备验证是否配置成功') {
			//               // 取消配网
			//               _this.cancelEsp();
			//               _this.$api.msg('已取消配网');
			//             } else {
			//               return;
			//             }
			//           } else if (res.cancel) {
			//             console.log('用户点击取消');
			//           }
			//         }
			//       });
			//     })
			//
			//   }
			// },


			destroyed: function() {
				console.log("destroyed----------")
				if (this.connId !== '') {
					uni.closeBLEConnection({
						deviceId: this.connId,
						success(res) {
							console.log(res)
						}
					})
				}

			},


		}
	}
</script>

<style lang='scss' scoped>

	.add-btn{
		/* position: fixed; */
		left: 30upx;
		right: 30upx;
		bottom: 16upx;
		z-index: 95;
		display: flex;
		align-items: center;
		justify-content: center;
		width: 690upx;
		height: 80upx;
		font-size: 32upx;
		color: #fff;
		background-color: #138f87;
		border-radius: 500rpx;
		margin-top: 320rpx;
		margin-left: -70rpx;
	}

	.modal {
		display: flex;
		justify-content: center;
	}
</style>